package de.slothsoft.android.authenticator;

import static android.accounts.AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE;
import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class AccountAuthenticator extends AbstractAccountAuthenticator {

    static final String DUMMY_TOKEN = "DUMMY_TOKEN"; //$NON-NLS-1$

    private final Authenticator oAuth2Authenticator;
    private final Context context;

    public AccountAuthenticator(Context context, Authenticator oAuth2Authenticator) {
	super(context);
	this.context = context;
	this.oAuth2Authenticator = oAuth2Authenticator;
    }

    @Override
    public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String tokenType,
	    String[] requiredFeatures, Bundle options) throws NetworkErrorException {
	Log.d("slothsoft", "addAccount(" + tokenType + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	return createLoginIntentBundle(response, accountType, null, tokenType, true, options);
    }

    @Override
    public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String tokenType, Bundle options)
	    throws NetworkErrorException {
	Log.d("slothsoft", "getAuthToken(" + tokenType + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	return createLoginIntentBundle(response, account.type, account.name, tokenType, false, options);
    }

    private Bundle createLoginIntentBundle(AccountAuthenticatorResponse response, String accountType,
	    String accountName, String tokenType, boolean newAccount, Bundle options) {
	final Intent intent = createLoginIntent(response, accountType, accountName, tokenType, newAccount, options);
	final Bundle bundle = new Bundle();
	bundle.putParcelable(AccountManager.KEY_INTENT, intent);
	return bundle;
    }

    private Intent createLoginIntent(AccountAuthenticatorResponse response, String accountType, String accountName,
	    String tokenType, boolean newAccount, Bundle options) {
	final Intent intent = new Intent(this.context, LoginActivity.class);
	intent.putExtra(KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
	intent.putExtra(LoginActivity.ARG_LOGIN_OPTIONS, new LoginActivity.LoginOptions(accountType, accountName,
		tokenType, this.oAuth2Authenticator, newAccount));
	intent.putExtra(LoginActivity.ARG_APPLICATION, options.getString(AuthenticationUtil.OPTION_APPLICATION));
	return intent;
    }

    @Override
    public String getAuthTokenLabel(String tokenType) {
	return tokenType;
    }

    @Override
    public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features)
	    throws NetworkErrorException {
	final Bundle result = new Bundle();
	result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
	return result;
    }

    @Override
    public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
	return null;
    }

    @Override
    public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options)
	    throws NetworkErrorException {
	return null;
    }

    @Override
    public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String tokenType,
	    Bundle options) throws NetworkErrorException {
	return null;
    }

}
